<?php
namespace Library\Config;

class Config {

    /**
     * 用来存放全局配置
     * @var array
     */
    public $configs = [];

    /**
     * 初始化配置文件
     * @param $config_file_path
     * @throws \Exception
     */
    public function __construct($config_file_path)
    {
        if(file_exists($config_file_path)) {
            $this->configs = include $config_file_path;
        } else {
            throw new \ErrorException('全局配置文件不存在');
        }
    }

    /**
     * 获取配置
     * @param $name
     * @return mixed
     * @throws \Exception
     */
    public function get($name)
    {
        $name = (string) $name;
        if(isset($this->configs[$name])) {
            return $this->configs[$name];
        } else {
            throw new \ErrorException('配置不存在');
        }
    }

    /**
     * 设置配置文件
     * @param $name
     * @param $val
     */
    public function set($name, $val)
    {
        $name = (string) $name;
        $this->configs[$name] = $val;
    }
}